/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views.Dialog;

import controllers.entity.Paper;
import controllers.process.CanBoProcess;
import controllers.process.PaperProcess;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import models.Constant;
import views.JPanel.TTCanBo;

/**
 *
 * @author HoaDuyen
 */
public class DlgThemPaper extends java.awt.Dialog {

    private CanBoProcess canBoProcess;
    private PaperProcess paperProcess;
    private int macanbo;
    private Paper paper;
    private TTCanBo parent;
    public DlgThemPaper(JFrame parent, boolean modal,int macanbo,Paper paper ) {
        super(parent, modal);
        this.parent=(TTCanBo)parent;
        this.paper=paper;
        this.setResizable(false);
        this.macanbo=macanbo;
        this.setLocation(Constant.LOCATE_XDLG, Constant.LOCATE_YDLG);
        canBoProcess=new CanBoProcess();
        paperProcess=new PaperProcess();
        initComponents();
        ok.setText("Thêm");
        if (paper != null) { // Dang o che do sua
            tentapchi.setText(paper.getTenTapChi());
            hesoif.setText(""+paper.getIF());
            chisoissn.setText(""+paper.getISSN());
            so.setText(""+paper.getSo());
            namxuatban.setText(""+paper.getThoiGian());
            txtGiaDinh.setText("Sửa thông tin Paper");
            ok.setText("Lưu");
            tenpaper.setText(paper.getTenBaiBao());
        }
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel13 = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        tentapchi = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        so = new javax.swing.JTextField();
        hesoif = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        namxuatban = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        tenpaper = new javax.swing.JTextField();
        chisoissn = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        txtGiaDinh = new javax.swing.JLabel();
        ok = new javax.swing.JButton();
        cancelcon = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jLabel14.setText("Tên tạp chí");

        jLabel15.setText("Số");

        jLabel17.setText("Hệ số IF");

        jLabel18.setText("Năm xuất bản");

        jLabel1.setText("Tên bài báo");

        tenpaper.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tenpaperActionPerformed(evt);
            }
        });

        jLabel16.setText("Chi số ISSN");

        javax.swing.GroupLayout jPanel13Layout = new javax.swing.GroupLayout(jPanel13);
        jPanel13.setLayout(jPanel13Layout);
        jPanel13Layout.setHorizontalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(tenpaper, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel14)
                            .addComponent(jLabel16))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chisoissn, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tentapchi, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel18)
                    .addComponent(jLabel15)
                    .addComponent(jLabel17))
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(so, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(namxuatban, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(hesoif, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18))
        );
        jPanel13Layout.setVerticalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tenpaper)
                    .addComponent(jLabel15)
                    .addComponent(so, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tentapchi)
                    .addComponent(jLabel14)
                    .addComponent(jLabel18)
                    .addComponent(namxuatban))
                .addGap(18, 18, 18)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(hesoif)
                            .addComponent(jLabel17)
                            .addComponent(chisoissn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24))
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        txtGiaDinh.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        txtGiaDinh.setText("Thêm Báo Cáo Khoa Học");

        ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_add.png"))); // NOI18N
        ok.setText("Thêm");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        cancelcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_delete.png"))); // NOI18N
        cancelcon.setText("Hủy");
        cancelcon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelconActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(141, 141, 141)
                        .addComponent(ok)
                        .addGap(34, 34, 34)
                        .addComponent(cancelcon))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jPanel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(94, 94, 94)
                        .addComponent(txtGiaDinh)))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtGiaDinh)
                .addGap(18, 18, 18)
                .addComponent(jPanel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(45, 45, 45)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok)
                    .addComponent(cancelcon))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the dialog
     */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
         if (tenpaper.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            tenpaper.selectAll();
            tenpaper.requestFocus();
            return;
        } 
          if (tentapchi.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            tentapchi.selectAll();
            tentapchi.requestFocus();
            return;
        } 
        if( (tentapchi.getText().equals("")) || 
            (hesoif.getText().equals("")) || 
            (chisoissn.getText().equals("")) ||
                (tenpaper.getText().equals(""))||
                (so.getText().equals("")) ||
                (namxuatban.getText().equals(""))){
            JOptionPane.showMessageDialog(this, "Không được bỏ trống !");
            return;
        }
        String ttentapchi=tentapchi.getText();
        float thesoIF,tchisoissn;
        int tnam,tso;
        try {
            thesoIF= Float.parseFloat(hesoif.getText());
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Nhập số thực !");
            hesoif.selectAll();
            hesoif.requestFocus();
            return;
        }
        if (thesoIF<0){
            JOptionPane.showMessageDialog(this, "Không được <0 !");
            hesoif.selectAll();
            hesoif.requestFocus();
            return;
        }
        try {
            tchisoissn= Float.parseFloat(chisoissn.getText());
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Nhập số thực !");
            chisoissn.selectAll();
            chisoissn.requestFocus();
            return;
        }
        if (tchisoissn<0){
             JOptionPane.showMessageDialog(this, "Không được <0 !");
            chisoissn.selectAll();
            chisoissn.requestFocus();
            return;
        }
        try {
            tso= Integer.parseInt(so.getText());
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Nhập số nguyên !");
            so.selectAll();
            so.requestFocus();
            return;
        }
        if (tso<0){
            JOptionPane.showMessageDialog(this, "Không được <0 !");
            so.selectAll();
            so.requestFocus();
            return;
        }
        try {
            tnam= Integer.parseInt(namxuatban.getText());
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Nhập số nguyên !");
            namxuatban.selectAll();
            namxuatban.requestFocus();
            return;
        }
        String tnamxuatban=namxuatban.getText();
        Integer namxuatban1=Integer.parseInt(tnamxuatban);
        if (namxuatban1>2012 ||namxuatban1<1900){
            JOptionPane.showMessageDialog(this, "Thông tin năm xuất bản sai rồi!");
            namxuatban.selectAll();
            namxuatban.requestFocus();
            return;
        }
        int dem = PaperProcess.getCountPaper()+1;
        String tmapaper=""+dem;
        String tenbaibao=tenpaper.getText();
        
            Paper ct=new Paper(tmapaper, macanbo, ttentapchi,tso,tnamxuatban,tchisoissn,thesoIF,tenbaibao);
            if (paper == null) { // Them moi sach
                if (PaperProcess.insertPaper(ct)) {
                    JOptionPane.showMessageDialog(this, "Thêm thành công!");
                    parent.loadListPaper();
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(this, "Failed!");
                }
            } else {
                //sua sach
                ct.setMaPaper(paper.getMaPaper());
                if (PaperProcess.updatePaper(ct)) {
                    JOptionPane.showMessageDialog(this, "Sửa thành công!");
                    parent.loadListPaper();
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(this, "Failed!");
                }
            
        }
    }//GEN-LAST:event_okActionPerformed

    private void cancelconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelconActionPerformed
        dispose();
    }//GEN-LAST:event_cancelconActionPerformed

    private void tenpaperActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tenpaperActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tenpaperActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelcon;
    private javax.swing.JTextField chisoissn;
    private javax.swing.JTextField hesoif;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JTextField namxuatban;
    private javax.swing.JButton ok;
    private javax.swing.JTextField so;
    private javax.swing.JTextField tenpaper;
    private javax.swing.JTextField tentapchi;
    private javax.swing.JLabel txtGiaDinh;
    // End of variables declaration//GEN-END:variables
}
